package com.lujianfei.netease_android_mvvm.main.impl;

import com.lujianfei.netease_android_mvvm.App;
import com.lujianfei.netease_android_mvvm.R;
import com.lujianfei.netease_android_mvvm.entity.DataBean;
import com.lujianfei.netease_android_mvvm.main.IMainMvvm;

import java.util.ArrayList;
import java.util.List;

import androidx.lifecycle.MutableLiveData;

public class MainViewModel extends androidx.lifecycle.ViewModel implements IMainMvvm.ViewModel {

    List<DataBean> dataListList = new ArrayList<>();
    List<DataBean> removedDataListList = new ArrayList<>();
    public MutableLiveData<UiState> uiState = new MutableLiveData<>();

    class MarkObject {
        int position;
        DataBean dataList;
    }

    public static class UiState {
        public List<DataBean> dataListList;
        public Boolean selectAll;
        public String title;
        public OnEditModeListener onEditModeListener;
        public Boolean updateSelectAll;
        public String toast;
        public RangeChanged rangeChangedPosition;

        public UiState(List<DataBean> dataListList,
                       Boolean selectAll,
                       String title,
                       OnEditModeListener onEditModeListener,
                       Boolean updateSelectAll,
                       String toast,
                       RangeChanged rangeChangedPosition) {
            this.dataListList = dataListList;
            this.selectAll = selectAll;
            this.title = title;
            this.onEditModeListener = onEditModeListener;
            this.updateSelectAll = updateSelectAll;
            this.toast = toast;
            this.rangeChangedPosition = rangeChangedPosition;
        }

        public interface OnEditModeListener {
            void onEditMode(boolean editmode);
        }

        public static class RangeChanged {
            public int position;
            public int count;

            public RangeChanged(int position, int count) {
                this.position = position;
                this.count = count;
            }

        }

        @Override
        public String toString() {
            return "UiState{" +
                    ", selectAll=" + selectAll +
                    ", title='" + title + '\'' +
                    ", onEditModeListener=" + onEditModeListener +
                    ", updateSelectAll=" + updateSelectAll +
                    ", toast='" + toast + '\'' +
                    ", rangeChangedPosition=" + rangeChangedPosition +
                    '}';
        }
    }

    private void emitUI(List<DataBean> dataListList,
                        Boolean selectAll,
                        String title,
                        UiState.OnEditModeListener onEditModeListener,
                        Boolean updateSelectAll,
                        String toast,
                        UiState.RangeChanged rangeChangedPosition) {
        uiState.setValue(new UiState(dataListList, selectAll, title, onEditModeListener, updateSelectAll, toast, rangeChangedPosition));
    }

    private void emitUIDataList(List<DataBean> dataListList) {
        emitUI(dataListList, null, null, null, null, null, null);
    }

    private void emitUISelectAll(Boolean selectAll) {
        emitUI(null, selectAll, null, null, null, null, null);
    }

    private void emitUITitle(String title) {
        emitUI(null, null, title, null, null, null, null);
    }

    private void emitUIGetEditMode(UiState.OnEditModeListener onEditModeListener) {
        emitUI(null, null, null, onEditModeListener, null, null, null);
    }

    private void emitUIUpdateSelectAll(Boolean updateSelectAll) {
        emitUI(null, null, null, null, updateSelectAll, null, null);
    }

    private void emitUIToast(String toast) {
        emitUI(null, null, null, null, null, toast, null);
    }

    private void emitUINotifyItemRangeChanged(UiState.RangeChanged rangeChangedPosition, String title,Boolean updateSelectAll) {
        emitUI(null, null, title, null, updateSelectAll, null, rangeChangedPosition);
    }

    public MainViewModel() {

    }

    @Override
    public void load() {
        for (int i = 0; i < 20; i++) {
            dataListList.add(new DataBean("title" + i, "summary" + i));
        }
        emitUIDataList(dataListList);
    }

    @Override
    public void setSelectAll(boolean selectAll) {
        emitUISelectAll(selectAll);

        int selectedCount = getSelectedCount();

        emitUITitle(App.getInstance().getResources().getQuantityString(R.plurals.selected, selectedCount, selectedCount));
    }

    @Override
    public void clickItem(final DataBean data) {
        emitUIGetEditMode(new UiState.OnEditModeListener() {
            @Override
            public void onEditMode(boolean editMode) {
                if (editMode) {
                    int position = dataListList.indexOf(data);
                    data.setChecked(!data.isChecked());
                    int quantity = getSelectedCount();
                    emitUINotifyItemRangeChanged(new UiState.RangeChanged(position, 1), App.getInstance().getResources().getQuantityString(R.plurals.selected, quantity, quantity),!isUnSelectAll());
                } else {
                    emitUIToast(data.getTitle() + " 被点击");
                }
            }
        });
    }

    @Override
    public void clickDelete() {
        removedDataListList.clear();
        for (DataBean data :
                dataListList) {
            if (data.isChecked()) {
                removedDataListList.add(data);
            }
        }
        dataListList.removeAll(removedDataListList);
        emitUIDataList(dataListList);
        int selectedCount = getSelectedCount();
        emitUITitle(App.getInstance().getResources().getQuantityString(R.plurals.selected, selectedCount, selectedCount));
        emitUIUpdateSelectAll(!isUnSelectAll());
    }

    private int getSelectedCount() {
        int selected = 0;
        for (DataBean dataList :
                dataListList) {
            if (dataList.isChecked()) {
                ++selected;
            }
        }
        return selected;
    }

    private boolean isUnSelectAll() {
        boolean unselectAll = true;
        for (DataBean dataList :
                this.dataListList) {
            if (dataList.isChecked()) {
                unselectAll = false;
                break;
            }
        }
        return unselectAll;
    }
}
